﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MediaPlayer.ViewModel
{
    class MyCommand : ICommand
    {
        #region fields
        private readonly Action execute;
        private readonly Func<bool> canExecute;
        #endregion

        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (this.canExecute != null)
                    CommandManager.RequerySuggested += value;
            }
            remove
            {
                if (this.canExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }

        public MyCommand(Action action)
            : this(action, null)
        {
        }

        public MyCommand(Action action, Func<bool> canExecute)
        {
            if (action == null)
                throw new ArgumentNullException("action");
            this.execute = action;
            this.canExecute = canExecute;
        }

        public void Execute(object parameter)
        {
            this.execute();
        }

        public bool CanExecute(object parameter)
        {
            return this.canExecute == null ? true : this.canExecute();
        }
    }
}
